<template>
  <div style="display: flex; justify-content: center; align-items: center">
    <el-form
      ref="FormData"
      :model="FormData"
      label-width="80px"
      class="demo-ruleForm"
      style="width: 50%"
      label-position="left"
      :rules="rules"
    >
      <el-form-item label="用户组名" prop="userGroup">
        <el-input v-model="FormData.userGroup" type="text"></el-input>
      </el-form-item>

      <el-form-item label="权限组" prop="permissionGroupId">
        <el-select v-model="FormData.permissionGroupId" style="width: 100%">
          <el-option
            v-for="item in group"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "../../utils/request";
import { ElNotification } from "element-plus";
export default {
  name: "userGroupAdd",
  data() {
    return {
      group:{},
      FormData: {
        userGroup: "",
        permissionGroupId: "",
      },
      rules: {
        userGroup: [
          { required: true, message: "用户组名不允许为空", trigger: "blur" },
        ],
        permissionGroupId: [
          { required: true, message: "权限组不允许为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(val) {
      this.$refs.FormData.validate((valid) => {
        if (valid) {
          axios.post("/UserGroup/add", this.FormData).then((res) => {
            if (res.data.status === 200) {
              ElNotification.success(res.data.message);
              this.$router.push({ name: "sys:userGroup:list" });
            } else {
              ElNotification.error("添加失败，请稍后再试");
            }
          });
        } else {
          return false;
        }
      });
    },

    // 获取权限组内容
    getPermissionGroupInfo() {
      axios.get("/permissionGroup/info").then((res) => {
        this.group = res.data.data.info;
      });
    },
  },
  mounted(){
    this.getPermissionGroupInfo();
  }
};
</script>
